// import Cesium from "../../node_modules/cesium";

import { basegeoserver } from "../../public/mapconfig";
import * as turf from "@turf/turf";
export function addWMSLayer(viewer, layername) {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.WebMapServiceImageryProvider({
      url: basegeoserver + "/geoserver/ANDINGQU/wms",
      layers: layername,
      parameters: {
        service: "WMS",
        version: "1.1.1",
        format: "image/png",
        srs: "EPSG:4326",
        transparent: true,
      },
    })
  );
}
export function showOrhideWMSLayer(viewer, layername, type) {
  let imglayer = viewer.imageryLayers._layers;
  imglayer.forEach((element) => {
    console.log(element);
    if (
      element._imageryProvider !== undefined &&
      element._imageryProvider._layers == layername
    ) {
      // console.log(element._imageryProvider._layers);
      element.show = type;
    }
  });
}
export function showOrhideBaseLayer(viewer, layername, type) {
  let imglayer1 = viewer.imageryLayers._layers;
  imglayer1.forEach((e) => {
    // console.log(e);
    if (
      e._imageryProvider !== undefined &&
      e._imageryProvider._layer == layername
    ) {
      // console.log(e._imageryProvider._layer);
      e.show = type;
    }
  });
}
export function cameraFlyto(viewer, lonlatheight) {
  viewer.camera.flyTo({
    destination: Cesium.Cartesian3.fromDegrees(
      lonlatheight[0],
      lonlatheight[1],
      lonlatheight[2]
    ),
    orientation: {
      heading: Cesium.Math.toRadians(0.0),
      pitch: Cesium.Math.toRadians(-45.0),
      roll: 0.0,
    },
    // complete: addYanmo(viewer)
  });
}

export function getlenLonlat(lonlat, len) {
  var positions = [];
  for (let index = 1; index < lonlat.length; index++) {
    var line = turf.lineString([lonlat[index - 1], lonlat[index]]);
    var from = turf.point(lonlat[index - 1]);
    var to = turf.point(lonlat[index]);
    var options = { units: "meters" };
    var distance = turf.distance(from, to, options);
    // positions.push(lonlat[0]);
    for (let i = 1; i < (distance / len).toFixed(0); i++) {
      var stop = len * i;
      var sliced = turf.lineSliceAlong(line, 0, stop, {
        units: "meters",
      });
      positions.push(sliced.geometry.coordinates[1]);
    }
  }

  return positions;
}
